package com.blockmeta.bbs.immodule.widget.chatboard;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;

import com.blockmeta.bbs.baselibrary.utils.permission.RequestPermissionUtil;
import com.blockmeta.bbs.businesslibrary.widget.emoji.EmojiEditText;
import com.blockmeta.bbs.businesslibrary.widget.emoji.OnMediaClickListener;
import com.blockmeta.bbs.immodule.R;
import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.app.TakePhotoImpl;
import org.devio.takephoto.model.InvokeParam;
import org.devio.takephoto.model.TContextWrap;
import org.devio.takephoto.model.TImage;
import org.devio.takephoto.model.TResult;
import org.devio.takephoto.permission.InvokeListener;
import org.devio.takephoto.permission.PermissionManager;
import org.devio.takephoto.permission.TakePhotoInvocationHandler;

import java.io.File;
import java.util.ArrayList;

/**
 * @author AirThor
 * @date 2020/3/27.
 */
public class NormalChatBoard extends BaseChatBoard implements TakePhoto.TakeResultListener, InvokeListener {
    private FrameLayout mFlSendImg,mFlTakePhoto,mFlSendMarket,mFlSendF10,mFlSendGift,mFlSendMedia;
    private CheckBox mCbEmoji,mCbSendImg,mCbTakePhoto,mCbSendMarket,mCbSendF10,mCbSendGift,mCbSendMedia;
    private int mCurrentArea = NO_AREA;
    private final static int EMOJI = 0;
    private final static int MARKET = 1;
    private final static int F10 = 2;
    private final static int MEDIA = 3;

    private OnMediaClickListener mOnMediaClickListener;

    private InvokeParam invokeParam;
    private TakePhoto takePhoto;
    private Uri imageUri;
    private View mView;
    @Override
    protected void init(View view) {
        super.init(view);
        mCbEmoji = view.findViewById(R.id.cb_send_emoji);
        mCbSendImg = view.findViewById(R.id.cb_send_img);
        mCbTakePhoto = view.findViewById(R.id.cb_take_photo);
        mCbSendMarket= view.findViewById(R.id.cb_send_market);
        mCbSendF10 = view.findViewById(R.id.cb_send_f10);
        mCbSendGift = view.findViewById(R.id.cb_send_gift);
        mCbSendMedia = view.findViewById(R.id.cb_media);

        mFlSendImg = view.findViewById(R.id.fl_send_img);
        mFlTakePhoto = view.findViewById(R.id.fl_take_photo);
        mFlSendMarket= view.findViewById(R.id.fl_send_market);
        mFlSendF10 = view.findViewById(R.id.fl_send_f10);
        mFlSendGift = view.findViewById(R.id.fl_send_gift);
        mFlSendMedia = view.findViewById(R.id.fl_media);

        mCbEmoji.setOnClickListener(this);
        mFlSendImg.setOnClickListener(this);
        mFlTakePhoto.setOnClickListener(this);
        mFlSendMarket.setOnClickListener(this);
        mFlSendF10.setOnClickListener(this);
        mFlSendGift.setOnClickListener(this);
        mFlSendMedia.setOnClickListener(this);

        setOnMediaClickListener(this);
    }

    @Override
    protected View getInputAreaView() {
        return getLayoutInflater().inflate(R.layout.input_area,null);
    }

    @Override
    protected EmojiEditText getInputView(View view) {
        return view.findViewById(R.id.edt_input);
    }

    @Override
    protected void resetToolAreaState() {
        mCurrentArea = NO_AREA;
        updateCurrentSelectedArea();
    }

    /**
     * 功能区应该展示哪种区域
     * @return 0发送表情 1发送行情 2发送F10 3发送媒体文件等等
     */
    @Override
    protected int whichToolAreaShow() {
        return mCurrentArea;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cb_send_emoji) {
            //发表情 展示表情功能区
            mCurrentArea = EMOJI;
            updateCurrentSelectedArea();
            showOrHideKeyboard();
        }else if (i == R.id.fl_send_market){
            //发行情 展示行情功能区
            mCurrentArea = MARKET;
            updateCurrentSelectedArea();
            showOrHideKeyboard();
        }else if (i == R.id.fl_send_f10){
            //发F10 展示F10功能区
            mCurrentArea = F10;
            updateCurrentSelectedArea();
            showOrHideKeyboard();
        }else if (i == R.id.fl_media){
            //发文件等等 展示媒体功能区
            mCurrentArea = MEDIA;
            updateCurrentSelectedArea();
            showOrHideKeyboard();
        }else if (i == R.id.fl_send_img){
            //选择图片
            mView = v;
            RequestPermissionUtil.requestStorage(new RequestPermissionUtil.OnPermissionGrantedListener() {
                @Override
                public void onPermissionGranted() {
                    getTakePhoto().onPickMultiple(9);

                }

            });

        }else if (i == R.id.fl_take_photo){
            //拍照
            mView = v;
            RequestPermissionUtil.requestStorage(new RequestPermissionUtil.OnPermissionGrantedListener() {
                @Override
                public void onPermissionGranted() {
                    File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    imageUri = Uri.fromFile(file);
                    getTakePhoto().onPickFromCapture(imageUri);

                }
            });
        }else if (i == R.id.fl_send_gift){
            //发红包
            mOnOutPutListener.onChatBoardEvent(KeyboardEvent.RED_ENVELOPE);
        }
    }

    /**
     * 更新当前展示的功能区域
     */
    private void updateCurrentSelectedArea(){
        switch (mCurrentArea){
            case EMOJI:
                mCbEmoji.setChecked(true);
                mCbSendMarket.setChecked(false);
                mCbSendF10.setChecked(false);
                mCbSendMedia.setChecked(false);
                break;
            case MARKET:
                mCbSendMarket.setChecked(true);
                mCbEmoji.setChecked(false);
                mCbSendF10.setChecked(false);
                mCbSendMedia.setChecked(false);
                break;
            case F10:
                mCbSendF10.setChecked(true);
                mCbSendMarket.setChecked(false);
                mCbEmoji.setChecked(false);
                mCbSendMedia.setChecked(false);
                break;
            case MEDIA:
                mCbSendMedia.setChecked(true);
                mCbSendMarket.setChecked(false);
                mCbSendF10.setChecked(false);
                mCbEmoji.setChecked(false);
                break;
            default:
                mCbEmoji.setChecked(false);
                mCbSendMarket.setChecked(false);
                mCbSendF10.setChecked(false);
                mCbSendMedia.setChecked(false);
                break;
        }
    }

    public void setOnMediaClickListener(OnMediaClickListener onMediaClickListener) {
        mOnMediaClickListener = onMediaClickListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getTakePhoto().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type= PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
        PermissionManager.handlePermissionsResult(getActivity(),type,invokeParam,this);
    }
    /**
     *  获取TakePhoto实例
     * @return
     */
    public TakePhoto getTakePhoto(){
        if (takePhoto==null){
            takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
        }
        return takePhoto;
    }

    @Override
    public void takeSuccess(TResult result) {

        ArrayList<TImage> images = result.getImages();
        for (int i = 0, size = images.size(); i < size ; i++) {
            TImage image = images.get(i);
            String compressPath;
            if(image.getFromType() == TImage.FromType.CAMERA) {
                compressPath = imageUri.getPath();
            }else {
                compressPath = image.getOriginalPath();
            }
            mOnMediaClickListener.onMediaClick(mView,compressPath);
        }
    }


    @Override
    public void takeFail(TResult result, String msg) {
    }

    @Override
    public void takeCancel() {
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type= PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)){
            this.invokeParam=invokeParam;
        }
        return type;
    }
}
